import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/apartment/api.dart";
import "package:ct_pa_majordomo/pages/transfer_station.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/state/common.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:provider/provider.dart";

class ApartmentForm extends StatefulWidget {
  const ApartmentForm({super.key});

  @override
  State<StatefulWidget> createState() => _ApartmentFormState();
  
}

class _ApartmentFormState extends State<ApartmentForm> {

  Map<String, dynamic> _params = {};
  int _memberId = 1;

  bool _validate() {
    if (_params["name"] == null || _params["name"] == "") return false;
    if (_params["phonenumber"] == null || _params["phonenumber"].length != 11) return false;
    if (_params["address"] == null || _params["address"] == "") return false;
    return true;
  }

  void _createApartment(commonVM) async {
    Map<String, dynamic> params = {
      ..._params,
    };
    await apartmentApi.createApartment(params);
    _getApartmentInfo(commonVM);
    Stacker.pushAndRemoveUntil(const TransferStation());
  }

  Future<void> _getApartmentInfo(commonVM) async {
    Map resData = await apartmentApi.getApartmentInfo();
    if (context.mounted) {
        print("apartInfo :>>> ${resData["data"]}");
        commonVM.setApartment(resData["data"]);
      }
    }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
        title: "创建公寓",
        child: Consumer<CommonState>(builder: (ctx, commonVM, child) {
          return ListView(
            children: [
              MenuBlock(
                items: [
                  MenuBlockItem(
                    text: "公寓名称",
                    showArrow: false,
                    suffix: TextFormField(
                      textAlign: TextAlign.right,
                      decoration: const InputDecoration(
                        hintText: "请输入",
                        hintStyle: TextStyle(color: ThemeVars.colorTextTer),
                        border: InputBorder.none,
                        filled: true,
                        fillColor: ThemeVars.colorBackground,
                      ),
                      initialValue: _params["name"],
                      onChanged: (val) {
                        setState(() {
                          _params["name"] = val;
                        });
                      },
                    ),
                  ),
                  MenuBlockItem(
                    text: "客服电话",
                    showArrow: false,
                    suffix: TextFormField(
                      textAlign: TextAlign.right,
                      decoration: const InputDecoration(
                        hintText: "请输入",
                        hintStyle: TextStyle(color: ThemeVars.colorTextTer),
                        border: InputBorder.none,
                        filled: true,
                        fillColor: ThemeVars.colorBackground,
                      ),
                      initialValue: _params["phonenumber"],
                      onChanged: (val) {
                        setState(() {
                          _params["phonenumber"] = val;
                        });
                      },
                    ),
                  ),
                  MenuBlockItem(
                    text: "所在城市",
                    showArrow: false,
                    suffix: TextFormField(
                      textAlign: TextAlign.right,
                      decoration: const InputDecoration(
                        hintText: "请输入",
                        hintStyle: TextStyle(color: ThemeVars.colorTextTer),
                        border: InputBorder.none,
                        filled: true,
                        fillColor: ThemeVars.colorBackground,
                      ),
                      initialValue: _params["address"],
                      onChanged: (val) {
                        setState(() {
                          _params["address"] = val;
                        });
                      },
                    ),
                  )
                ],
              ),
              SizedBox(height: 30,),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: ThemeVars.paddingPage),
                child: Button(
                  type: ButtonType.primary,
                  onPressed: _validate() ? () {
                    _createApartment(commonVM);
                  } : null,
                  child: Text("创建公寓"),
                ),
              )
            ],
          );
        })
    );
  }
}
